{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "# 範囲"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "## 範囲リテラル\n",
    "\n",
    "* `..`\n",
    "    * 範囲に右の式の結果を含む(以下)\n",
    "* `...`\n",
    "    * 範囲に右の式の結果を含まない(未満)\n",
    "    \n",
    "生成されたものは範囲オブジェクトと呼ばれるRangeクラスのインスタンス"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "true\n",
      "false\n"
     ]
    }
   ],
   "source": [
    "puts (1..5).include?(5)\n",
    "puts (1...5).include?(5)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "## 同値判定、包含判定\n",
    "\n",
    "* ==\n",
    "    * 同値判定\n",
    "* ===\n",
    "* include?\n",
    "    * 包含判定"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "true\n",
      "範囲オブジェクトと数値の5は同値ではない\n",
      "false\n"
     ]
    }
   ],
   "source": [
    "puts (1..5) === 5\n",
    "\n",
    "puts \"範囲オブジェクトと数値の5は同値ではない\"\n",
    "puts (1..5) == 5\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "## for式に範囲オブジェクトを適用"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a\n",
      "b\n",
      "c\n",
      "d\n",
      "a\n",
      "b\n",
      "c\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "\"a\"...\"d\""
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "for i in \"a\"..\"d\" do\n",
    "  puts i\n",
    "end\n",
    "\n",
    "for k in \"a\"...\"d\" do\n",
    "  puts k\n",
    "end"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "## 配列の添字演算子に範囲オブジェクトを適用"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[\"c\", \"d\", \"e\"]\n",
      "[\"c\", \"d\"]\n",
      "[\"c\"]\n"
     ]
    }
   ],
   "source": [
    "a = [\"a\", \"b\", \"c\", \"d\", \"e\"]\n",
    "\n",
    "puts a[2,3]\n",
    "puts a[2..3]\n",
    "puts a[2...3]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "## 文字列の添字演算子に範囲オブジェクトを適用"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "c\n",
      "cde\n",
      "cd\n"
     ]
    }
   ],
   "source": [
    "a = \"abcdef\"\n",
    "puts a[2]\n",
    "puts a[2..4]\n",
    "puts a[2...4]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "## case式"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "基本\n",
      "1\n",
      "\n",
      "複数マッチする場合は最初にマッチしたwhen節が実行される\n",
      "\"foo\"\n",
      "\n",
      "変数に代入する\n",
      "2\n"
     ]
    }
   ],
   "source": [
    "puts \"基本\"\n",
    "case 1\n",
    "  when 1 then\n",
    "    p 1\n",
    "end\n",
    "puts\n",
    "\n",
    "puts \"複数マッチする場合は最初にマッチしたwhen節が実行される\"\n",
    "case \"aa\"\n",
    "  when \"aa\" then\n",
    "    p \"foo\"\n",
    "  when \"aa\" then\n",
    "    p \"bar\"\n",
    "end\n",
    "puts\n",
    "\n",
    "puts \"変数に代入する\"\n",
    "a = 10\n",
    "b = case a\n",
    "  when 5 then\n",
    "    1\n",
    "  else\n",
    "    2\n",
    "end\n",
    "puts b"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "### 複数の条件式の指定\n",
    "\n",
    "* カンマで区切ると複数の条件を指定できる\n",
    "    * or条件となる"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "2"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "case 3\n",
    "  when 1,2 then; p 1\n",
    "  when 3,4 then; p 2\n",
    "end"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "### 条件式に範囲を指定\n",
    "\n",
    "* caseでは === 演算子が実行されている"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "2"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "case 7\n",
    "  when 1..5 then; p 1\n",
    "  when 6..9 then; p 2\n",
    "end"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "## while式とuntil式\n",
    "\n",
    "* while\n",
    "    * 条件を満たす間処理を繰り返す\n",
    "* until\n",
    "    * 条件を満たしていない間処理を繰り返す"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "iが4になるまで繰り返す\n",
      "0\n",
      "1\n",
      "2\n",
      "3\n",
      "4\n",
      "k=5がtrueになるまで繰り返す\n",
      "1\n",
      "2\n",
      "3\n",
      "4\n",
      "5\n"
     ]
    }
   ],
   "source": [
    "puts \"iが4になるまで繰り返す\"\n",
    "i = 0\n",
    "while (0..4) === i do\n",
    "  p i\n",
    "  i += 1\n",
    "end\n",
    "\n",
    "puts \"k=5がtrueになるまで繰り返す\"\n",
    "k = 0\n",
    "until k==5 do\n",
    "  k += 1\n",
    "  p k\n",
    "end"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "## 後置while\n",
    "\n",
    "* begin節は条件にかかわらず実行される\n",
    "  * 最初に一回かならず実行されることを強調する"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "iが4になるまで繰り返す\n",
      "0\n",
      "1\n",
      "2\n",
      "3\n",
      "4\n"
     ]
    }
   ],
   "source": [
    "puts \"iが4になるまで繰り返す\"\n",
    "i = 0\n",
    "begin\n",
    "  p i\n",
    "  i += 1\n",
    "end while(1..4) === i"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "whileの条件に当てはまらなくてもbegin endの中の処理は実行される\n",
      "0\n",
      "i = 1\n",
      "\n",
      "1\n",
      "2\n",
      "3\n",
      "4\n",
      "5\n",
      "i = 6\n",
      "0\n",
      "1\n",
      "2\n",
      "3\n",
      "i = 4\n"
     ]
    }
   ],
   "source": [
    "puts \"whileの条件に当てはまらなくてもbegin endの中の処理は実行される\"\n",
    "i = 0\n",
    "begin\n",
    "  p i\n",
    "  i += 1\n",
    "end while(2..5) === i\n",
    "puts \"i = #{i}\"\n",
    "puts\n",
    "\n",
    "i = 1\n",
    "begin\n",
    "  p i\n",
    "  i += 1\n",
    "end while(2..5) === i\n",
    "puts \"i = #{i}\"\n",
    "\n",
    "i = 0\n",
    "begin\n",
    "  p i\n",
    "  i += 1\n",
    "end while(1..3) === i\n",
    "puts \"i = #{i}\"\n",
    "  "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "## while 修飾子\n",
    "\n",
    "* 条件を満たす間式が評価される"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "whileの条件が評価されてからiに1が加算され、その結果が表示される\n",
      "\n",
      "1\n",
      "2\n",
      "3\n",
      "4\n",
      "\n"
     ]
    }
   ],
   "source": [
    "puts \"whileの条件が評価されてからiに1が加算され、その結果が表示される\"\n",
    "i = 0\n",
    "p i +=1 while (1..4) === i\n",
    "puts\n",
    "\n",
    "i = 0\n",
    "p i +=1 while (0..3) === i\n",
    "puts\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Ruby 2.3.1",
   "language": "ruby",
   "name": "ruby"
  },
  "language_info": {
   "file_extension": ".rb",
   "mimetype": "application/x-ruby",
   "name": "ruby",
   "version": "2.3.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
